package com.example.cushion.realize.frame.splash;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.cushion.R;
import com.example.cushion.base.BaseFragment;

import org.itzheng.and.baseutils.ui.UIUtils;

/**
 * Created by root on 17-5-7.
 */

public class SplashFragment extends BaseFragment {
    private View rootView;
    /**
     * 图片
     */
    private ImageView iv;
    /**
     * 点击跳过
     */
    private View layoutSkip;
    /**
     * 剩余时间
     */
    private TextView tvCountdown;
    /**
     * 文字，跳过
     */
    private TextView tvSkip;
    /**
     * 剩余时间
     */
    private long countdownTime = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_splash, null);
            iv = rootView.findViewById(R.id.iv);
            layoutSkip = rootView.findViewById(R.id.layoutSkip);
            tvCountdown = rootView.findViewById(R.id.tvCountdown);
            tvSkip = rootView.findViewById(R.id.tvSkip);
            layoutSkip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    skip();
                }
            });
        }
        return rootView;
    }

    Handler handler = new Handler(Looper.getMainLooper());
    /**
     * 结束要做的事情
     */
    Runnable endRunnable = new Runnable() {
        @Override
        public void run() {
            if (mOnEndListener != null) {
                mOnEndListener.onEnd(SplashFragment.this);
            }
        }
    };
    /**
     * 更新倒计时
     */
    Runnable uptCountdownRunnable = new Runnable() {
        @Override
        public void run() {
            if (tvCountdown != null) {
                tvCountdown.setText("" + countdownTime);
            }
            countdownTime--;
            //每1秒钟进行更新
            uptCountDown();
        }
    };

    /**
     * 跳过
     */
    private void skip() {
        handler.removeCallbacks(uptCountdownRunnable);
        handler.removeCallbacks(endRunnable);
        handler.post(endRunnable);
    }

    /**
     * 开始执行
     */
    public void start() {
        showSkipLayout(false);
        initAnim();
    }

    /**
     * 广告停留时间
     */
    public static final long AD_STAY_TIME = 4000;
    /**
     * 启动停留时间
     */
    public static final long SPLASH_TIME = 1000;
    /**
     * 1秒钟等于1000毫秒
     */
    public static final long TIME_SECOND = 1000;
    /**
     * 是否显示广告
     */
    private boolean isShowAd = true;

    /**
     * 图片动画效果
     */
    private void initAnim() {
        AnimationSet set = new AnimationSet(false);

        ScaleAnimation scale = new ScaleAnimation(1, 1, 1, 1, Animation.RELATIVE_TO_SELF, 1f,
                Animation.RELATIVE_TO_SELF, 1f);
        scale.setDuration(SPLASH_TIME);
        scale.setFillAfter(true);

        AlphaAnimation alpha = new AlphaAnimation(0, 1);
        alpha.setDuration(100);
        alpha.setFillAfter(true);

        // set.addAnimation(rotate);
        set.addAnimation(scale);
        set.addAnimation(alpha);

        set.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (!isShowAd) {
                    //没设置广告页的时候，直接进入主界面
                    skip();
                } else {
                    //设置广告页的时候
                    startAd();
                }

            }
        });
        //开始动画
        rootView.startAnimation(set);

    }

    /**
     * 开启广告
     */
    private void startAd() {
        iv.setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher_round));
        handler.postDelayed(endRunnable, AD_STAY_TIME);
        countdownTime = AD_STAY_TIME / TIME_SECOND;
        countdownTime--;
        //先设置数据，然后再1秒更新
        handler.post(uptCountdownRunnable);
        showSkipLayout(true);

    }

    /**
     * 显示或隐藏跳过按钮
     *
     * @param isVisibility
     */
    private void showSkipLayout(boolean isVisibility) {
        if (layoutSkip != null) {
            layoutSkip.setVisibility(isVisibility ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 进行倒计时
     */
    private void uptCountDown() {
        //这个肯定是1秒更新一次
        handler.postDelayed(uptCountdownRunnable, TIME_SECOND);
    }

    private OnEndListener mOnEndListener;

    /**
     * 动画执行完毕的回调
     *
     * @param onEndListener
     */
    public void setOnEndListener(OnEndListener onEndListener) {
        mOnEndListener = onEndListener;
    }
}
